package com.xpgk.mvc.ddc.statistic.zdStatistic.zdMonthStatistic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.statistic.zdStatistic.zdDailyStastic.ZdDailyStasticSQL;
import com.xpgk.mvc.ddc.statistic.zdStatistic.zdDailyStastic.ZdDailyStasticXLS;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;

public class ZdMonthStasticController extends BaseController{

	public void index(){
		render("zdMonthStastic.html");
	}
	
	public void getList(){
		
		String monthStr = getPara("bussdate");//2018-04
		String orgSql = getSqlMy(ZdMonthStasticSQL.sqlId_selOrgan);
		List<Record> orgList =Db.find(orgSql,monthStr,getCUserOrganIds());
		String sql  = getSqlMy(ZdMonthStasticSQL.sqlId_getOnemonthTotalList);
		List<Record> records = new ArrayList<>();
		if(orgList!=null && orgList.size()>0){
			for(int i = 0;i<orgList.size();i++){
				String organIds = orgList.get(i).getStr("organids");
				Record  r =  Db.findFirst(sql,organIds ,monthStr);
			
				r.set("organname", orgList.get(i).getStr("orgname"));
				r.set("organids", orgList.get(i).getStr("organids"));
				records.add(r);
			}
		}
		renderJson(records);
	}
	
	public   void  excel() 
	{
		User user = User.dao.findById(getCUserIds());
		Organization org = Organization.dao.findById(getCUserOrganIds());
		String monthStr = getPara("bussdate");//2018-04
		String orgSql = getSqlMy(ZdMonthStasticSQL.sqlId_selOrgan);
		int zcgd = 0;
		int zygd = 0;
		int bggd = 0;
		int bhpgd = 0;
		int bzjgd = 0;
		int zxgd = 0;
		List<Record> orgList =Db.find(orgSql,monthStr,getCUserOrganIds());
		String sql  = getSqlMy(ZdMonthStasticSQL.sqlId_getOnemonthTotalList);
		List<Record> records = new ArrayList<>();
		if(orgList!=null && orgList.size()>0){
			for(int i = 0;i<orgList.size();i++){
				String organIds = orgList.get(i).getStr("organids");
				Record  r =  Db.findFirst(sql,organIds ,monthStr);
				r.set("organname", orgList.get(i).getStr("orgname"));
				r.set("organids", orgList.get(i).getStr("organids"));
				records.add(r);
			}
		}
		if(records!=null && records.size()>0){
			for(Record record:records){
				if(record.getBigDecimal("zcgd")!=null && !record.getBigDecimal("zcgd").equals("")){
					zcgd = zcgd+record.getBigDecimal("zcgd").intValue();
				}
				if(record.getBigDecimal("zygd")!=null && !record.getBigDecimal("zygd").equals("")){
					zygd = zygd+record.getBigDecimal("zygd").intValue();
				}
				if(record.getBigDecimal("bggd")!=null && !record.getBigDecimal("bggd").equals("")){
					bggd = bggd+record.getBigDecimal("bggd").intValue();
				}
				if(record.getBigDecimal("bhpgd")!=null && !record.getBigDecimal("bhpgd").equals("")){
					bhpgd = bhpgd+record.getBigDecimal("bhpgd").intValue();
				}
				if(record.getBigDecimal("bzjgd")!=null && !record.getBigDecimal("bzjgd").equals("")){
					bzjgd = bzjgd+record.getBigDecimal("bzjgd").intValue();
				}
				if(record.getBigDecimal("zxgd")!=null && !record.getBigDecimal("zxgd").equals("")){
					zxgd = zxgd+record.getBigDecimal("zxgd").intValue();
				}
			}
		}
		Record r = new Record();
		r.set("organname","合计")
		.set("zcgd",zcgd)
		.set("zygd",zygd)
		.set("bggd",bggd)
		.set("bhpgd",bhpgd)
		.set("bzjgd",bzjgd)
		.set("zxgd",zxgd);
		records.add(r);
		//搜索条件字符串
		String  search="时间："+monthStr+" 导出人："+user.getStr(User.column_name)+" 导出机构："+org.getStr(Organization.column_name);
		render(new ZdDailyStasticXLS(records,search,"支队月报表"+monthStr));
	}
}
